package com.example.module_service.device

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.entity.PatrolEntity
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.adapter.PatrolAdapter
import com.example.module_service.databinding.ActivityPatrolBinding
import com.example.module_service.viewmodel.ServerViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_DEVICE_PAGE)
class PatrolActivity : BaseMvvmActivity<ActivityPatrolBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_patrol

    private val mViewModel: ServerViewModel by viewModels()

    private lateinit var patrolAdapter: PatrolAdapter
    private val detaillist = mutableListOf<PatrolEntity>()

    override fun initViews() {
        super.initViews()

        // 初始化适配器
        patrolAdapter = PatrolAdapter(detaillist)

        // 设置RecyclerView
        mDataBinding.rv.run {
            layoutManager = LinearLayoutManager(this@PatrolActivity)
            adapter = patrolAdapter
        }

        // 设置添加按钮点击事件
        mDataBinding.fabAdd.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_ADD_POINT_PAGE).navigation()
        }

        // 设置适配器的点击监听
        patrolAdapter.setOnItemClickListener(object : PatrolAdapter.OnItemClickListener {
            override fun onItemClick(view: View, patrolEntity: PatrolEntity) {
                ARouter.getInstance()
                    .build(RouterPath.PATH_POINT_DETAIL_PAGE)
                    .withString("pointId", patrolEntity.id.toString())
                    .withString("pointName", patrolEntity.pointName)
                    .withString("projectName", patrolEntity.projectId)
                    .withString("pointLocation", patrolEntity.pointLocation)
                    .withString("patrolType", patrolEntity.patrolType)
                    .withString("patrolStandard", patrolEntity.patrolStandard)
                    .withString("status", patrolEntity.status ?:"启用")
                    .navigation()
            }
        })
    }

    override fun loadData() {
        super.loadData()
        mViewModel.getPatrol()
    }

    override fun observeDataChang() {
        super.observeDataChang()
        mViewModel.getpatrolSuccess.observe(this) {
            detaillist.clear()
            detaillist.addAll(it)
            patrolAdapter.notifyDataSetChanged()
        }
    }
}
